package com.by.controller;

import com.by.bean.ResultBean;
import com.by.bean.User;
import com.by.group.V;
import com.by.query.LoginRequest;
import com.by.service.UserService;
import com.by.utils.Assert;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author :LYJ
 * @version : 1.0
 * @date : 2025/2/19 20:05
 * @desc :
 * @since : JDK17
 * >>>>>>> li
 */
@RestController
@Api(tags = "用户管理")
@RequestMapping("/api")
public class UserController extends BaseController {
    @Autowired
    UserService userService;

    @ApiOperation("检查账户是否存在")
    @ApiImplicitParam(name = "account", value = "账户", required = true, dataTypeClass = String.class, paramType = "query")
    @GetMapping("/user/check")
    public ResultBean check(@RequestParam(required = true) String account) {
        int rs = userService.checkUser(account);
        String tmp = rs == 0 ? "no!" : "yes!";
        return success("检查账户是否存在", tmp);
    }

    @PostMapping("/supplier/userSave")
    @ApiOperation("用户账户保存")
    public ResultBean save(@Validated @RequestBody User user, BindingResult result) {
        V.validate(result);
        int res = userService.save(user);
        Assert.isZero(res, "保存失败");
        return success("success", "保存成功");
    }

    /**
     * 查询账户对应的角色
     *
     * @param loginRequest
     * @param result
     * @return
     */
    @PostMapping("/supplier/queryRole")
    @ApiOperation("查询账户对应的角色")
    public ResultBean queryRole(@Validated @RequestBody LoginRequest loginRequest, BindingResult result) {
        V.validate(result);
        List<String> list = userService.queryUserRole(loginRequest.getAccount());
        return success("success", list);
    }
}
